﻿Imports MapWindow.Interfaces
Imports MapWinGIS

Public Class graph

    Private _mapWin As IMapWin

    Public Sub DrawGraph(ByVal mapWin As IMapWin)

        _mapWin = mapWin

        'populate the comboBox with a list of ski paths
        Dim pathNames As New ArrayList()

        For Each lay As Layer In mapWin.Layers

            If lay.LayerType = eLayerType.LineShapefile Then

                Dim pathShapefile As Shapefile = CType(lay.GetObject(), Shapefile)

                pathNames.Add(CType(pathShapefile.CellValue(0, 0), String))

            End If

        Next
        ComboBox1.DataSource = pathNames

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        '(1) get the selected ski path shapefile
        Dim pathName As String = ComboBox1.SelectedValue
        For Each lay As Layer In _mapWin.Layers

            If lay.Name = pathName Then

                Dim pathShapefile As Shapefile = CType(lay.GetObject(), Shapefile)

                '(2) calculate elevation profile of the shapefile
                Dim profileTable As DataTable = ski.CalculateElevationProfile(pathShapefile, ski.DemGrid)

                '(3) draw the graph for this shapefile
                Dim rowCount As Integer = profileTable.Rows.Count
                Dim distanceArray(rowCount - 1) As Double
                Dim elevationArray(rowCount - 1) As Double
                Dim slopeArray(rowCount - 1) As Double

                For i As Integer = 0 To rowCount - 1
                    distanceArray(i) = profileTable.Rows(i)(0)
                    elevationArray(i) = profileTable.Rows(i)(1)
                    slopeArray(i) = profileTable.Rows(i)(2)
                Next

                'Refresh the ski graph
                skiGraph.GraphPane.CurveList.Clear()


                skiGraph.GraphPane.AddCurve("Elevation Profile", distanceArray, elevationArray, Drawing.Color.Blue)
                skiGraph.GraphPane.XAxis.Title.Text = "Distance (meters)"
                skiGraph.GraphPane.YAxis.Title.Text = "Elevation (meters)"
                skiGraph.AxisChange()

                'add the slope graph
                Dim slopeCurve As ZedGraph.LineItem = skiGraph.GraphPane.AddCurve("Slope", distanceArray, slopeArray, Drawing.Color.Red)
                slopeCurve.IsY2Axis = True
                skiGraph.GraphPane.Y2Axis.Title.Text = "slope"
                skiGraph.AxisChange()

                'set the graph title
                skiGraph.GraphPane.Title.Text = ComboBox1.SelectedValue

                skiGraph.Refresh()
                skiGraph.AxisChange()

                Exit For
            End If

        Next


        'convert the table to two arrays
        

    End Sub

End Class